package cn.hsp.tiktok.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.Animation
import android.view.animation.LinearInterpolator
import android.view.animation.RotateAnimation
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import cn.hsp.tiktok.R
import cn.hsp.tiktok.VideoBean
import cn.hsp.tiktok.utils.NumUtils
import cn.hsp.tiktok.utils.OnVideoControllerListener
import cn.hsp.tiktok.view.autolink.AutoLinkHerfManager
import cn.hsp.tiktok.view.autolink.AutoLinkTextView
import com.airbnb.lottie.LottieAnimationView
import com.bumptech.glide.Glide

class ControllerView(context: Context, attrs: AttributeSet?) : RelativeLayout(context, attrs),
    View.OnClickListener {
    private var listener: OnVideoControllerListener? = null
    private var videoData: VideoBean? = null
    private lateinit var ivHead: ImageView
    private lateinit var ivComment: View
    private lateinit var ivShare: View
    private lateinit var rlLike: View
    private lateinit var ivLike: IconFontTextView
    private lateinit var ivFocus: View

    private lateinit var ivHeadAnim: CircleImageView
    private lateinit var animationView: LottieAnimationView
    private lateinit var tvNickname: TextView
    private lateinit var tvLikecount: TextView
    private lateinit var tvCommentCount: TextView
    private lateinit var tvSharecount: TextView
    private lateinit var rlRecord: ViewGroup
    private lateinit var autoLinkTextView: AutoLinkTextView

//    tvLikecount!!.text = NumUtils.numberFilter(videoData.likeCount)
//    tvCommentCount!!.text = NumUtils.numberFilter(videoData.commentCount)
//    tvSharecount!!.text = NumUtils.numberFilter(videoData.shareCount)

    private fun init() {
        val rootView = LayoutInflater.from(context).inflate(R.layout.view_controller, this)
        ivHead = rootView.findViewById(R.id.ivHead)
        ivComment = rootView.findViewById(R.id.ivComment)
        ivShare = rootView.findViewById(R.id.ivShare)
        rlLike = rootView.findViewById(R.id.rlLike)
        ivLike = rootView.findViewById(R.id.ivLike)
        rlRecord = rootView.findViewById(R.id.rlRecord)
        tvNickname = rootView.findViewById(R.id.tvNickname)
        autoLinkTextView = rootView.findViewById(R.id.autoLinkTextView)
        tvLikecount = rootView.findViewById(R.id.tvLikecount)
        tvCommentCount = rootView.findViewById(R.id.tvCommentCount)
        tvSharecount = rootView.findViewById(R.id.tvSharecount)
        animationView = rootView.findViewById(R.id.animationView)
        ivFocus = rootView.findViewById(R.id.ivFocus)
        ivHeadAnim = rootView.findViewById(R.id.ivHeadAnim)
        ivHead.setOnClickListener(this)
        ivComment.setOnClickListener(this)
        ivShare.setOnClickListener(this)
        rlLike.setOnClickListener(this)
        ivFocus.setOnClickListener(this)
        setRotateAnim()
    }

    fun setVideoData(videoData: VideoBean) {
        this.videoData = videoData
        Glide.with(context).load(videoData.userBean.head).into(ivHead)
        tvNickname.text = "@" + videoData.userBean.nickName
        AutoLinkHerfManager.setContent(videoData.content, autoLinkTextView)
        Glide.with(context).load(videoData.userBean.head).into(ivHeadAnim)
        tvLikecount.text = NumUtils.numberFilter(videoData.likeCount)
        tvCommentCount.text = NumUtils.numberFilter(videoData.commentCount)
        tvSharecount.text = NumUtils.numberFilter(videoData.shareCount)
        animationView.setAnimation("like.json")

        //点赞状态
        if (videoData.isLiked) {
            ivLike.setTextColor(resources.getColor(R.color.color_FF0041))
        } else {
            ivLike.setTextColor(resources.getColor(R.color.white))
        }

        //关注状态
        if (videoData.isFocused) {
            ivFocus.visibility = GONE
        } else {
            ivFocus.visibility = VISIBLE
        }
    }

    fun setListener(listener: OnVideoControllerListener?) {
        this.listener = listener
    }

    override fun onClick(v: View) {
        if (listener == null) {
            return
        }
        when (v.id) {
            R.id.ivHead -> listener!!.onHeadClick()
            R.id.rlLike -> {
                listener!!.onLikeClick()
                like()
            }
            R.id.ivComment -> listener!!.onCommentClick()
            R.id.ivShare -> listener!!.onShareClick()
            R.id.ivFocus -> if (!videoData!!.isFocused) {
                videoData!!.isLiked = true
                ivFocus.visibility = GONE
            }
        }
    }

    /**
     * 点赞动作
     */
    fun like() {
        if (!videoData!!.isLiked) {
            //点赞
            animationView.visibility = VISIBLE
            animationView.playAnimation()
            ivLike.setTextColor(resources.getColor(R.color.color_FF0041))
        } else {
            //取消点赞
            animationView.visibility = INVISIBLE
            ivLike.setTextColor(resources.getColor(R.color.white))
        }
        videoData!!.isLiked = !videoData!!.isLiked
    }

    /**
     * 循环旋转动画
     */
    private fun setRotateAnim() {
        val rotateAnimation = RotateAnimation(
            0f, 359f,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f
        )
        rotateAnimation.repeatCount = Animation.INFINITE
        rotateAnimation.duration = 8000
        rotateAnimation.interpolator = LinearInterpolator()
        rlRecord!!.startAnimation(rotateAnimation)
    }

    init {
        init()
    }
}